package com.imooc.jdbc.common;

import java.sql.*;

public final class ConnectionUtil {

    private static String url = "jdbc:mysql://localhost:3306/message_board";
    private static String user = "root";
    private static String password = "hello1234";


    private ConnectionUtil() {}

    static  {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("can not find com.mysql.jdbc.Driver");
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            System.out.println("connect db error");
            e.printStackTrace();
        }
        return null;
    }

    public static void release (ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException sqlEx) {
                sqlEx.printStackTrace();
            }
            rs = null;
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) {
                sqlEx.printStackTrace();
            }
            stmt = null;
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException sqlEx) {
                sqlEx.printStackTrace();
            }
            conn = null;
        }
    }
}
